package com.tojaoomy.car.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/upload")
public class FileUploadController {

	@RequestMapping(value = "/index",method={RequestMethod.GET})
	public ModelAndView index(){
		ModelAndView view = new ModelAndView("upload.ftl");
		return view;
	}
	
	@RequestMapping(value = "/submit")
	public Object submit(@RequestParam(value = "file", required = false) MultipartFile file,HttpSession session){
		System.out.println(System.getProperty("catalina.base"));
		File saveFile = new File(this.getClass().getResource("/").getPath().replace("WEB-INF/classes/", "save/") + System.currentTimeMillis() + "_" + file.getOriginalFilename());
		try {
			FileUtils.writeByteArrayToFile(saveFile, file.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	
}
